package cc.cc4414.cc02.chat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.cc4414.cc02.chat.VO.FriendVO;
import cc.cc4414.cc02.chat.VO.GroupVO;
import cc.cc4414.cc02.chat.VO.MessageSendVO;
import cc.cc4414.cc02.chat.service.MessageSendService;
import cc.cc4414.cc02.core.controller.BaseController;
import cc.cc4414.cc02.core.result.ResultVO;

/**
 * @author cc 2018年10月4日
 */
@Validated
@RestController
@RequestMapping("/messageSend")
public class MessageSendController extends BaseController {

	@Autowired
	private MessageSendService messageSendService;

	/**
	 * 查询所有好友及聊天相关信息
	 * 
	 * @return 查询到的数据
	 */
	@GetMapping("friendList")
	public ResultVO<List<FriendVO>> friendList() {
		return new ResultVO<>(messageSendService.friendList(getUser().getId()));
	}

	/**
	 * 查询所有所在群组及聊天相关信息
	 * 
	 * @return 查询到的数据
	 */
	@GetMapping("groupList")
	public ResultVO<List<GroupVO>> groupList() {
		return new ResultVO<>(messageSendService.groupList(getUser().getId()));
	}

	/**
	 * 发送消息给好友
	 * 
	 * @param id
	 *            发送对象的id
	 * @param message
	 *            消息内容
	 * @return
	 */
	@PostMapping("sendFriend")
	public ResultVO<Object> sendFriend(@RequestParam Long id, @RequestParam String message) {
		messageSendService.send(getUser(), id, message, 1);
		return new ResultVO<>();
	}

	/**
	 * 发送消息给群组
	 * 
	 * @param id
	 *            发送对象的id
	 * @param message
	 *            消息内容
	 * @return
	 */
	@PostMapping("sendGroup")
	public ResultVO<Object> sendGroup(@RequestParam Long id, @RequestParam String message) {
		messageSendService.send(getUser(), id, message, 2);
		return new ResultVO<>();
	}

	/**
	 * 倒序查询与好友聊天的消息
	 * 
	 * @param id
	 *            聊天对象的id
	 * @param start
	 *            从第几条开始
	 * @param size
	 *            查询的数量
	 * @return 聊天数据
	 */
	@GetMapping("getFriendMessage")
	public ResultVO<List<MessageSendVO>> getFriendMessage(@RequestParam Long id, @RequestParam int start,
			@RequestParam int size) {
		return new ResultVO<>(messageSendService.getMessage(getUser().getId(), id, start, size, 1));
	}

	/**
	 * 倒序查询群聊天的消息
	 * 
	 * @param id
	 *            聊天对象的id
	 * @param start
	 *            从第几条开始
	 * @param size
	 *            查询的数量
	 * @return 聊天数据
	 */
	@GetMapping("getGroupMessage")
	public ResultVO<List<MessageSendVO>> getGroupMessage(@RequestParam Long id, @RequestParam int start,
			@RequestParam int size) {
		return new ResultVO<>(messageSendService.getMessage(getUser().getId(), id, start, size, 2));
	}

	/**
	 * 确认已读消息
	 * 
	 * @param id
	 *            聊天对象的id
	 * @param messageSendId
	 *            消息发送id
	 * @return 是否成功
	 */
	@PostMapping("readFriendMessage")
	public ResultVO<Object> readFriendMessage(@RequestParam Long id, @RequestParam Long messageSendId) {
		messageSendService.readMessage(getUser().getId(), id, messageSendId, 1);
		return new ResultVO<>();
	}

	/**
	 * 确认已读消息
	 * 
	 * @param id
	 *            聊天对象的id
	 * @param messageSendId
	 *            消息发送id
	 * @return 是否成功
	 */
	@PostMapping("readGroupMessage")
	public ResultVO<Object> readGroupMessage(@RequestParam Long id, @RequestParam Long messageSendId) {
		messageSendService.readMessage(getUser().getId(), id, messageSendId, 2);
		return new ResultVO<>();
	}

}
